<?php
/**
 * Created by PhpStorm.
 * User: thanatos
 * Date: 2017/10/30
 * Time: 下午2:02
 */

namespace common\helpers;


use yii\base\Model;
use yii\helpers\Json;

/**
 * Class JsonRes  用于返回JSON数据
 * @property string $msg   请求返回的提示信息 OK
 * @property array  $data  返回数组
 * @package common\components
 */
class AjaxHelper extends Model
{
    private $error = '0';
    private $msg   = 'OK';
    private $data  = [];
    
    public function __construct(array $config = [])
    {
        parent::__construct($config);
    }
    
    public function setMsg($message)
    {
        $this->error = '-1';
        $this->msg = $message;
    }
    
    public function setData($data)
    {
        $this->data = $data;
    }
    
    
    public function __toString()
    {
        if( $this->error != 0 ){
            return Json::encode(['error'=>$this->error, 'msg'=>$this->msg,'data'=>$this->data]);
        }else{
            return Json::encode(['success' => true, 'msg'=>$this->msg,'data'=>$this->data]);
        }
    }
    
}